package com.eatle.persistent.generator;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
import static org.mybatis.generator.internal.util.messages.Messages.getString;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.IntrospectedTable;

/**
 *@Description: 重命名 selectByExample
 */
public class RenameSlectByExamplePlugin extends PluginAdapter
{
	private String searchString;

	private String replaceString;

	private Pattern pattern;

	/**
     * 
     */
	public RenameSlectByExamplePlugin()
	{
	}

	public boolean validate(List<String> warnings)
	{

		searchString = properties.getProperty("searchString"); //$NON-NLS-1$
		replaceString = properties.getProperty("replaceString"); //$NON-NLS-1$

		boolean valid = stringHasValue(searchString)
				&& stringHasValue(replaceString);

		if (valid)
		{
			pattern = Pattern.compile(searchString);
		}
		else
		{
			if (!stringHasValue(searchString))
			{
				warnings.add(getString("ValidationError.18", //$NON-NLS-1$
						"RenameExampleClassPlugin", //$NON-NLS-1$
						"searchString")); //$NON-NLS-1$
			}
			if (!stringHasValue(replaceString))
			{
				warnings.add(getString("ValidationError.18", //$NON-NLS-1$
						"RenameExampleClassPlugin", //$NON-NLS-1$
						"replaceString")); //$NON-NLS-1$
			}
		}

		return valid;
	}

	@Override
	public void initialized(IntrospectedTable introspectedTable)
	{
		String oldType = introspectedTable.getSelectByExampleStatementId();
		Matcher matcher = pattern.matcher(oldType);
		oldType = matcher.replaceAll(replaceString);

		introspectedTable.setSelectByExampleStatementId(oldType);
	}
}
